package client

import (
	"github.com/golang/glog"
	"fmt"
	"cloud_worm/worm/client/docker"
	"cloud_worm/worm/client/monitor"
	"cloud_worm/worm/config"
	"cloud_worm/util/proto/ujson"
	"cloud_worm/util/uconst"
)

func (a *Agent) HandleAutoConfig(data []byte, localIP string)  {
	var reqMsg ujson.MsgAutoConfigReq
	if err := reqMsg.UnmarshalJSON(data); err != nil {
		glog.Errorf("MsgAutoConfigReq unmarshal json error: %s", err.Error())
		return
	}

	if len(reqMsg.Id) != uconst.AgentIdLen {
		glog.Error(fmt.Sprintf("input agent id error: %s", reqMsg.Id))
		return
	}

	switch reqMsg.NotifyType {
	// cmd monitor key rule
	case ujson.NtCmdRule:
		var confFile ujson.ConfigFilePayLoad
		if err := confFile.UnmarshalJSON(reqMsg.PayLoad); err != nil {
			glog.Errorf("ConfigFilePayLoad unmarshal json error: %s", err.Error())
			return
		}
		if err := monitor.UpdateCmdWhiteKey(confFile.FileURL, confFile.MD5); err != nil {
			glog.Error(fmt.Sprintf("update cmd white key error: %s", err.Error()))
			return
		}
		glog.Infof("update cmd white key config")
		//	file monitor file rule
	case ujson.NtFileRule:
		var confFile ujson.ConfigFilePayLoad
		if err := confFile.UnmarshalJSON(reqMsg.PayLoad); err != nil {
			glog.Errorf("ConfigFilePayLoad unmarshal json error: %s", err.Error())
			return
		}
		if err := monitor.UpdateFileRule(confFile.FileURL, confFile.MD5); err != nil {
			glog.Error(fmt.Sprintf("update file list error: %s", err.Error()))
			return
		}
		glog.Infof("update file list key config")
		//	cmd monitor enable
	case ujson.NtCmdMonitor:
		var enableInfo ujson.EnablePayLoad
		if err := enableInfo.UnmarshalJSON(reqMsg.PayLoad); err != nil {
			glog.Errorf("EnablePayLoad unmarshal json error: %s", err.Error())
			return
		}
		if enableInfo.Enable == true {
			if docker.ContainInfoInst.SystemType == docker.NormalHost ||
				docker.ContainInfoInst.SystemType == docker.DockerHost {
				if err := monitor.StartCmdMonitor(localIP); err != nil {
					glog.Errorf("start cmd monitor error: %s", err.Error())
				}
			}
		} else if enableInfo.Enable == false {
			monitor.StopCmdMonitor()
		}
		glog.Infof("update cmd monitor config: %v", enableInfo.Enable)
		//	file monitor enable
	case ujson.NtFileMonitor:
		var enableInfo ujson.EnablePayLoad
		if err := enableInfo.UnmarshalJSON(reqMsg.PayLoad); err != nil {
			glog.Errorf("EnablePayLoad unmarshal json error: %s", err.Error())
			return
		}
		if enableInfo.Enable == true {
			if err := monitor.StartFileMonitor(localIP); err != nil {
				glog.Errorf("start file monitor error: %s", err.Error())
			}
		} else if enableInfo.Enable == false {
			monitor.StopFileMonitor()
		}
		glog.Infof("update file monitor config: %v", enableInfo.Enable)
		// log collector enable
	case ujson.NtLogCollector:
		var enableInfo ujson.EnablePayLoad
		if err := enableInfo.UnmarshalJSON(reqMsg.PayLoad); err != nil {
			glog.Errorf("EnablePayLoad unmarshal json error: %s", err.Error())
			return
		}
		if enableInfo.Enable == true {
			if err := monitor.StartLogWatcher(localIP); err != nil {
				glog.Errorf("start log collector error: %s", err.Error())
			}
		} else if enableInfo.Enable == false {
			monitor.StopLogWatcher()
		}
		glog.Infof("update log collector config: %v", enableInfo.Enable)
		//	log collector file rule
	case ujson.NtLogCollectorRule:
		var confFile ujson.ConfigFilePayLoad
		if err := confFile.UnmarshalJSON(reqMsg.PayLoad); err != nil {
			glog.Errorf("ConfigFilePayLoad unmarshal json error: %s", err.Error())
			return
		}
		if err := monitor.UpdateCollectorRule(confFile.FileURL, confFile.MD5); err != nil {
			glog.Errorf("update log collector rule error: %s", err.Error())
			return
		}
		glog.Infof("update log collector rule list.")
	default:
		glog.Warningf("auto config req msg notify type unknown: %d", reqMsg.NotifyType)
	}

	return
}

func (a *Agent) Monitor(localIP string) error {

	monitorConfig := config.CfgInst.StartConfig.(*config.ClientConfig).Monitor

	// 如果需要启动，则启动对应的monitor
	if monitorConfig.CmdEnable {
		if docker.ContainInfoInst.SystemType == docker.NormalHost ||
			docker.ContainInfoInst.SystemType == docker.DockerHost {
			if err := monitor.StartCmdMonitor(localIP); err != nil {
				glog.Errorf("From config start cmd monitor error: %s", err.Error())
			} else {
				glog.Info("Start cmd monitor from config.")
			}
		}
	}
	if monitorConfig.FileEnable {
		if err := monitor.StartFileMonitor(localIP); err != nil {
			glog.Errorf("From config start file monitor error: %s", err.Error())
		} else {
			glog.Info("Start file monitor from config.")
		}
	}
	if monitorConfig.LogCollector {
		if err := monitor.StartLogWatcher(localIP); err != nil {
			glog.Errorf("From config start log watcher error: %s", err.Error())
		} else {
			glog.Info("Start log watcher from config.")
		}
	}

	return nil
}
